package com.chencs.biz.product.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.product.model.SimpleProductModel;
import com.chencs.biz.product.service.FavoritesService;
import com.chencs.common.grant.controller.RoleController;
import com.chencs.vo.biz.product.FavoritesModel;
import com.chencs.vo.biz.product.ProductModel;
import framework.model.ResponseOptUtil;
import org.omg.CORBA.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by fangzehua on 2017/6/30.
 * 收藏夹商品
 */
@RestController
@RequestMapping("/favorites")
public class FavoritesController {
    Logger logger = LoggerFactory.getLogger(FavoritesController.class);

    @Autowired
    private FavoritesService favoritesService;

    @RequestMapping(value = "/syncFavorites", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String syncFavorites(){
        if(favoritesService.syncFavorites()){
            return ResponseOptUtil.commonSucess();
        }else{
            return ResponseOptUtil.commonMsgError("收藏夹同步失败");
        }
    }

    @RequestMapping(value = "/getFavoriteList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getFavoriteList(){

        List<FavoritesModel> list = favoritesService.getFavoriteList();
        if(list.isEmpty()){
            return ResponseOptUtil.commonMsgError("获取收藏夹列表失败");
        }else{
            return JSON.toJSONString(list);
        }
    }

    /**
     * 获取收藏夹
     * @param code
     * @return
     */
    @RequestMapping(value = "/findFavoritesModelByCode", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String findFavoritesModelById(@RequestBody String code){
        FavoritesModel favoritesModel =  favoritesService.findFavoritesByCode(code);

        if (favoritesModel!=null){
            return  JSON.toJSONString(favoritesModel);
        }else{
            return ResponseOptUtil.commonMsgError("获取收藏夹失败");
        }
    }


    /**
     * 修改收藏夹
     * @param model
     * @return
     */

    @RequestMapping(value = "/updateFavoritesByModel", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String findFavoritesModelById(@RequestBody FavoritesModel model){

        try {
            favoritesService.updateFavoritesByModel(model);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("修改收藏夹失败");
        }
    }




    @RequestMapping(value = "/clear_pro", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String clear_pro(@RequestBody String code){
            try {
                favoritesService.clear_pro();
                return ResponseOptUtil.commonSucess();
            }catch (Exception e){
                return ResponseOptUtil.commonMsgError("清理商品失敗");
            }

    }

    @RequestMapping(value = "/syncFavoritesProduct", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String syncFavoritesProduct(@RequestBody String code){
        logger.debug("syncFavoritesProduct",code);
        int flag = favoritesService.syncFavoritesProduct(code);
        if(flag==0){
            return ResponseOptUtil.commonSucess();
        }else if(flag ==-1){
            return ResponseOptUtil.commonMsgError("请补全分类");
        }else if(flag == -3){
            return ResponseOptUtil.commonMsgError("选品库没商品,先去淘宝联盟添加！");
        }else{
            return ResponseOptUtil.commonMsgError("收藏夹商品同步失败");
        }

    }

    @RequestMapping(value = "/getSimpleProduct", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String getSimpleProduct(@RequestBody String pid){

        SimpleProductModel model = favoritesService.getSimpleProduct(pid);
        if (model!=null){
            return JSON.toJSONString(model).toString();
        }else{
           return ResponseOptUtil.commonMsgError("商品获取失败");
        }
    }


    @RequestMapping(value = "/getProductByCidForList", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String getProductByCidForList(@RequestBody String pid){

        SimpleProductModel model = favoritesService.getSimpleProduct(pid);
        if (model!=null){
            return JSON.toJSONString(model).toString();
        }else{
            return ResponseOptUtil.commonMsgError("商品获取失败");
        }
    }


    @RequestMapping(value = "/getProduct", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String getProduct(){

        List<ProductModel> list = favoritesService.getProduct();
        if (!list.isEmpty()){
            return JSON.toJSONString(list).toString();
        }else{
            return ResponseOptUtil.commonMsgError("商品获取失败");
        }
    }


    @RequestMapping(value = "/setHostKey", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public String setHostKey(){
        favoritesService.setHostKey();
        return ResponseOptUtil.commonMsgError("商品获取失败");

     }




  //  http://openapi.qingtaoke.com/hot?app_key=8qwMgSJx&v=1.0&t=1
}
